﻿function addEvents() {
    var event = $("#event");
    if (event.val() == ""){
        return;
    }
    var objEvent = { "event": event.val(), "isFinish": false }
    var data = getData();
    data.push(objEvent);
    saveData(data);
    showData();
    event.val("");
}

function getData() {
    var data = localStorage.getItem("eventList")
    if (data != null) {
        return JSON.parse(data);
    }
    else {
        return [];
    }
}

function saveData(data) {
    localStorage.setItem("eventList", JSON.stringify(data));
}

function showData() {
    var table = $("#eventList");
    var table2 = $("#donelist");
    var count = $("#thingNum");
    var count2 = $("#thingNum2");
    var number = 0;
    var number2 = 0;
    var data = localStorage.getItem("eventList");
    if (data != null) {
        var eventList = JSON.parse(data);
        var str = "";
        var str2 = "";
        for (var i = 0; i < eventList.length; i++) {
            if (eventList[i].isFinish) {
                number++;
                str += "<li class=\"finished\"><input class=\"check\" checked=\"checked\"  type=\"checkbox\" onclick=\"changeStatue(" + i + ")\" />" + eventList[i].event + "<a onclick=\"deleteEvent(" + i + ")\">-</a></li>";
            }
            else {
                number2++;
                str2 += "<li><input class=\"check\" type=\"checkbox\" onclick=\"changeStatue(" + i + ")\" />" + eventList[i].event + "<a onclick=\"deleteEvent(" + i + ")\">-</a></li>";
            }
        }
        table.html(str2);
        table2.html(str);
        count.html(number2);
        count2.html(number);
    }
    else {
        table.html("");
    }
}
$(document).ready(function () {
    showData();
})

function deleteEvent(index) {
    var data = getData();
    data.splice(index, 1);
    saveData(data);
    showData();
}

function changeStatue(index) {
    var data = getData();
    if (data[index].isFinish) {
        data[index].isFinish = false;
    }
    else {
        data[index].isFinish = true;
    }
    saveData(data);
    showData();
}




